package com.zpark.user.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zpark.user.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author zhangbing
 * @Date 2024/4/15 11:49  服务降级  默认fallback
 **/

@RestController
@RequestMapping("/consumer")
@Slf4j
@DefaultProperties(defaultFallback = "defaultFallBack")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    //DiscoveryClient 专门负责服务发现
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/{id}")
   //@HystrixCommand(fallbackMethod = "queryByIdFallBack")
    @HystrixCommand
    public String findById(@PathVariable Long id){

       /* if(id==1){
            throw new RuntimeException("太忙了");
        }*/
        String url="http://localhost:10010/user/"+id;
        url="http://User-Service2104/user/"+id; // User-Service2104
        return restTemplate.getForObject(url,String.class);
    }

    public String queryByIdFallBack(Long id){
        log.error("查询用户失败，id",id);

        return "对不起，网络拥挤了";
    }


    @DeleteMapping("/{id}")
    @HystrixCommand
    public String delById(){
        return null;
    }

    public String defaultFallBack(){

        return "默认提示：对不起，网络拥挤了";
    }


}
